﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Threading.Tasks;
using System.Configuration;
using System.Net;
using System.Text;
using System.IO;
using WindowsLive;
using System.Data;
using System.IO;

namespace goMatic
{
    public partial class Default1 : System.Web.UI.Page
    {
        const string LoginCookie = "webauthtoken";

        // Initialize the WindowsLiveLogin module.
        static WindowsLiveLogin wll = new WindowsLiveLogin(true);

        protected static string AppId = wll.AppId;
        //protected string UserId;
        protected string CustomerId;

        protected void Page_Load(object sender, EventArgs e)
        {
            /* If the user token has been cached in a site cookie, attempt
               to process it and extract the user ID. */

            HttpRequest req = HttpContext.Current.Request;
            HttpCookie loginCookie = req.Cookies[LoginCookie];

            if (loginCookie != null)
            {
                string token = loginCookie.Value;

                if (!string.IsNullOrEmpty(token))
                {
                    WindowsLiveLogin.User user = wll.ProcessToken(token);

                    if (user != null)
                    {
                        //UserId = user.Id;

                        dbEntities oDB = new dbEntities();
                        if (oDB.Customers.Count(t => t.liveid == user.Id) > 0)
                        {
                            CustomerId = oDB.Customers.First(t => t.liveid == user.Id).customerid;
                            ViewState.Add("CustomerID", CustomerId);

                            Response.Redirect("~/Start.aspx");
                        }
                        else
                        {
                            Response.Redirect("~/Registration.aspx");
                        }

                    }
                }
                else
                    Label1.Text = "Sie müssen sich mit Ihrem Windows Live Account anmelden.";
            }
            else
                Label1.Text = "Sie müssen sich mit Ihrem Windows Live Account anmelden.";
        }




    }
}